<?php

namespace App\Http\Controllers\Backend\Setting;

use App\Events\SettingLogEvent;
use App\Http\Requests\Backend\Setting\SettingDocumentRequest;
use App\Models\SettingDocument;
use App\Models\SettingLog;
use App\Http\Controllers\Controller;

class SettingDocumentController extends Controller
{
    use \App\Library\Traits\SettingLog;
    const SETTING_TABLE = 'setting_document';
    public function __construct()
    {
        $this->middleware('auth:backend');
        $this->user = auth('backend')->user();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $info = SettingDocument::getDocumentList();
        return success($info);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SettingDocumentRequest $request)
    {

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $settingDocument  = new SettingDocument();
        $settingDocument->setConnection('backend');
        $documentInfo = $settingDocument->select('id','type','document_name','document_content')->where('id',$id)->first();
        return success($documentInfo);

    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(SettingDocumentRequest $request, $id)
    {
        $updateData = $request->only(['channel_code','document_name','document_content','is_use_url']);
        if(isset($updateData['is_use_url'])){
            if($updateData['is_use_url']){
                $updateData['is_use_url'] = SettingDocument::USE_URL_TRUE;
            }else{
                $updateData['is_use_url'] = SettingDocument::USE_URL_FALSE;
            }
            //把这个id的同类型的is_use_url都设置
            $documentInfo = SettingDocument::select('type')->where('id',$id)->first();
            SettingDocument::where('type',$documentInfo['type'])->update(['is_use_url' => $updateData['is_use_url']]);

        }
        $info = SettingDocument::where('id',$id)->update($updateData);
        $this->setting_log($this->user->id,self::SETTING_TABLE,$id,SettingLog::LOG_UPDATE,$updateData);
        return updated('');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = SettingDocument::where('id',$id)->update(['document_name'=>'','document_content' => json_encode([])]);
        if($result){
            $this->setting_log($this->user->id,self::SETTING_TABLE,$id,SettingLog::LOG_DELETE);
            return deleted();
        }else{
            return failed('删除失败');
        }
    }
}
